<template>
  <div class="app-container">
    <t-form
      :model="queryParams"
      ref="queryRef"
      layout="inline"
      v-show="showSearch"
      class="mb20"
    >
      <t-form-item label="公告标题" prop="noticeTitle">
        <t-input
          v-model="queryParams.noticeTitle"
          placeholder="请输入公告标题"
          clearable
          style="width: 200px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="操作人员" prop="createBy">
        <t-input
          v-model="queryParams.createBy"
          placeholder="请输入操作人员"
          clearable
          style="width: 200px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="类型" prop="noticeType">
        <t-select
          v-model="queryParams.noticeType"
          placeholder="公告类型"
          clearable
          style="width: 200px"
        >
          <t-option
            v-for="dict in sys_notice_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          theme="primary"
          @click="handleAdd"
          v-hasPermi="['system:notice:add']"
          >新增
          <template #icon>
            <t-icon name="add" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="success"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:notice:edit']"
          >修改
          <template #icon>
            <t-icon name="edit" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:notice:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      v-loading="loading"
      :data="noticeList"
      row-key="noticeId"
      :columns="columns"
      @select-change="handleSelectionChange"
    >
      <template #noticeType="scope">
        <dict-tag :options="sys_notice_type" :value="scope.row.noticeType" />
      </template>
      <template #status="scope">
        <dict-tag :options="sys_notice_status" :value="scope.row.status" />
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
      </template>
      <template #action="scope">
        <t-button
          variant="text"
          theme="primary"
          @click="handleUpdate(scope.row)"
          v-hasPermi="['system:notice:edit']"
          >修改
          <template #icon>
            <t-icon name="edit" />
          </template>
        </t-button>
        <t-button
          variant="text"
          theme="primary"
          @click="handleDelete(scope.row)"
          v-hasPermi="['system:notice:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <t-dialog :title="title" v-model:visible="open" width="780px" attach="body">
      <t-form ref="noticeRef" :data="form" :rules="rules" label-width="80px">
        <t-row>
          <t-col :span="6" class="mb8">
            <t-form-item label="公告标题" prop="noticeTitle">
              <t-input
                v-model="form.noticeTitle"
                placeholder="请输入公告标题"
              />
            </t-form-item>
          </t-col>
          <t-col :span="6" class="mb8">
            <t-form-item label="公告类型" prop="noticeType">
              <t-select v-model="form.noticeType" placeholder="请选择">
                <t-option
                  v-for="dict in sys_notice_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></t-option>
              </t-select>
            </t-form-item>
          </t-col>
          <t-col :span="12" class="mb8">
            <t-form-item label="状态">
              <t-radio-group v-model="form.status">
                <t-radio
                  v-for="dict in sys_notice_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                >
                </t-radio>
              </t-radio-group>
            </t-form-item>
          </t-col>
          <t-col :span="12" class="mb8">
            <t-form-item label="内容">
              <editor v-model="form.noticeContent" :min-height="192" />
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="primary" @click="submitForm">确 定</t-button>
          <t-button theme="default" @click="cancel">取 消</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup name="Notice">
import {
  listNotice,
  getNotice,
  delNotice,
  addNotice,
  updateNotice,
} from "@/api/system/notice";

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
  "sys_notice_status",
  "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const columns = ref([
  {
    colKey: "row-select",
    type: "multiple",
    title: "选择",
    width: 55,
  },
  {
    colKey: "serial-number",
    title: "序号",
    width: 100,
  },
  { colKey: "noticeId", title: "公告标题", ellipsis: true, width: 200 },
  { colKey: "noticeType", title: "公告类型", width: 100, align: "center" },
  { colKey: "status", title: "状态", width: 100, align: "center" },
  { colKey: "createBy", title: "创建者", width: 160, align: "center" },
  { colKey: "createTime", title: "创建时间", width: 160, align: "center" },
  { colKey: "action", title: "操作", width: 260, align: "center" },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    noticeTitle: undefined,
    createBy: undefined,
    status: undefined,
  },
  rules: {
    noticeTitle: [
      { required: true, message: "公告标题不能为空", trigger: "blur" },
    ],
    noticeType: [
      { required: true, message: "公告类型不能为空", trigger: "change" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listNotice(queryParams.value).then((response) => {
    noticeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    noticeId: undefined,
    noticeTitle: undefined,
    noticeType: undefined,
    noticeContent: undefined,
    status: "0",
  };
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  ids.value = selectedRowKeys;
  single.value = selectedRowKeys.length != 1;
  multiple.value = !selectedRowKeys.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加公告";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();
  const noticeId = row.noticeId || ids.value;
  getNotice(noticeId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改公告";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate().then((valid) => {
    if (valid) {
      if (form.value.noticeId != undefined) {
        updateNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addNotice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const noticeIds = row.noticeId || ids.value;
  proxy.$modal
    .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
    .then(function () {
      return delNotice(noticeIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
